package com.ldk.api.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ldk.api.ApiVersion;
import com.ldk.api.ErrorCode;
import com.ldk.api.ResponseObject;
import com.ldk.api.ResponseResult;
import com.ldk.base.framework.util.ConvertUtil;
import com.ldk.framework.entity.Dictdata;
import com.ldk.framework.entity.HelpDoc;
import com.ldk.framework.service.IHelpDocService;

/**
 * 帮助模块
 * @author xiangwl
 * 2017年2月18日
 * com.ldk.api.handler
 * HelpDocRequestHandler
 */
public class HelpDocRequestHandler extends BaseRequestHandler {
	
	@Autowired
	private IHelpDocService helpDocService;
	
	/**
	 * 获取帮助分类
	 * @param parms{platform}
	 * @param platform 平台值
	 * @return
	 */
	@ApiVersion(value={1})
	public ResponseObject getHelpTypeList(Map<String, Object> parms){
		ResponseObject response = ResponseResult.create("help", "getHelpTypeList");
		
		if(!checkRequireParms(parms, "platform")){
			return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
		}
		
		List<Dictdata> list = getDictdataList(3);//type=3
		if(list == null || list.isEmpty()){
			response = ResponseResult.exception(response, ErrorCode.HELP_TYPE_EMPTY);
		}
		else{
			List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
			for (Dictdata dictdata : list) {
				Map<String, Object> item = new HashMap<String, Object>();
				item.put("typeId", dictdata.getDataValue());
				item.put("typeName", dictdata.getDataName());
				
				result.add(item);
			}
			response = ResponseResult.success(response, result);
		}
		
		return response;
	}
	
	/**
	 * 获取帮助内容列表
	 * @param parms {type}
	 * @param typeId 帮助内容分类
	 * @return
	 */
	@ApiVersion(value={1})
	public ResponseObject getHelpDocList(Map<String, Object> parms){
		ResponseObject response = ResponseResult.create("help", "getHelpDocList");
		
		if(!checkRequireParms(parms, "platform", "typeId")){
			return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
		}
		String platform = ConvertUtil.parseString(parms.get("platform"));
		String typeId = ConvertUtil.parseString(parms.get("typeId"));
		
		HelpDoc query = new HelpDoc();
		query.setEnabled(true);
		query.setHelpType(typeId);
		query.setPlatform(platform);
		
		List<HelpDoc> list = helpDocService.selectList("", query);
		if(list == null || list.isEmpty()){
			response = ResponseResult.exception(response, ErrorCode.HELP_DOC_EMPTY);
		}
		else{
			response = ResponseResult.success(response, list);
		}
		
		return response;
	}
	
	/**
	 * 获取帮助内容信息
	 * @param parms {helpId}
	 * @param helpId 帮助Id
	 * @return
	 */
	@ApiVersion(value={1})
	public ResponseObject getHelpDoc(Map<String, Object> parms){
		ResponseObject response = ResponseResult.create("help", "getHelpDoc");
		
		if(!checkRequireParms(parms, "helpId")){
			return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
		}
		long helpId = ConvertUtil.parseLong(parms.get("helpId"));
		HelpDoc query = new HelpDoc();
		query.setHelpId(helpId);
		
		HelpDoc doc = helpDocService.selectByKey("", query);
		if(doc == null){
			response = ResponseResult.exception(response, ErrorCode.HELP_DOC_CONTENT_EMPTY);
		}
		else{
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("helpId", doc.getHelpId());
			result.put("title", doc.getTitle());
			result.put("content", doc.getContent());
			
			response = ResponseResult.success(response, result);
		}
		
		return response;
	}
	
}
